<?php

namespace Ylb;
class Ylb
{
    private $app_cust_id;
    private $app_cust_pwd;


    public function __construct($options)
    {
        $this->app_cust_id = isset($options['app_cust_id']) ? $options['app_cust_id'] : '';
        $this->app_cust_pwd = isset($options['app_cust_pwd']) ? $options['app_cust_pwd'] : '';

    }

//获取签名
    private function getSignature()
    {
        $post_data = [
            'app_cust_id' => $this->app_cust_id,
            'app_cust_pwd' => $this->app_cust_pwd,
        ];
        $post_data = json_encode($post_data);
        $data = $this->send_post("https://api.gateway.letoiot.com/gateway/api/getSignature", $post_data, null);
        $data=json_decode($data,true);
        if($data['code']==0){
            $re_data=$data['data']['signature'];
        }

        return $re_data;
    }

//获取在线状态
    public function getOnlineStatus($param)
    {
        $post_data = [
            'devNo' => $param['devno']
        ];
        $post_data = json_encode($post_data);
        $data = $this->send_post("https://api.gateway.letoiot.com/speaker/search-device-online-state-v2", $post_data, $this->getSignature());

        return json_decode($data,true);
    }
    //播放语音
    //$type 支付类型
    // 1.支付宝
    // 2 微信
    // 3 云支付
    // 4 余额支付
    // 5 微信储值
    // 6 微信买单
    // 7 银联刷卡
    // 8,9,25哆啦宝
    public function addvoice($deviceid, $price, $type)
    {
        $data = $this->curl_get("https://api.gateway.letoiot.com/speaker/add-v2.php?id={$deviceid}&price={$price}&pt={$type}&ttl=60", $this->getSignature());

        return json_decode($data,true);
    }

//设置音量
    public function vol($deviceid, $vol)
    {
        $data = $this->curl_get("https://api.gateway.letoiot.com/speaker/add-v2.php?id={$deviceid}&vol={$vol}", $this->getSignature());

        return json_decode($data,true);
    }

    //绑定云喇叭
    public function bind($deviceid,$uid)
    {
        $descs = urlencode("绑定喇叭");
        $data = $this->curl_get("https://api.gateway.letoiot.com/speaker/bind-v2.php?id={$deviceid}&m=1&uid={$uid}&descs=" . $descs, $this->getSignature());

        return json_decode($data,true);
    }

    //解绑云喇叭
    public function unbind($deviceid, $uid)
    {
        $descs = urlencode("解绑喇叭");
        $data = $this->curl_get("https://api.gateway.letoiot.com/speaker/bind-v2.php?id={$deviceid}&m=0&uid={$uid}&descs=" . $descs, $this->getSignature());

        return json_decode($data,true);
    }

    //代理商喇叭查询
    public function speakeragency($deviceid)
    {
        $data = $this->curl_get("https://api.gateway.letoiot.com/speaker/list_speakeragency_v2.php?id={$deviceid}", $this->getSignature());

        return json_decode($data,true);
    }

    private function send_post($url, $post_data, $signature)
    {
        $headers = array("Content-type: application/json",);
        if (!empty($signature)) {
            $headers[1] = "Authorization:" . $signature;
        }
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
//        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话

        return $result;
    }

    private function curl_get($url, $signature)
    {
        $headers = array("Content-type: application/json",);
        if (!empty($signature)) {
            $headers[1] = "Authorization:" . $signature;
        }
        $testurl = $url;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $testurl);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        //参数为1表示传输数据，为0表示直接输出显示。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //参数为0表示不带头文件，为1表示带头文件
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}